package com.sophialex.health.record.common.dialog;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.sophialex.health.record.common.Messages;
import com.sophialex.health.system.frame.assist.CommonAssist;
import com.sophialex.health.system.frame.assist.TextAssHelp;
import com.sophialex.health.system.frame.resource.RcpResourceManager;
import com.sophialex.health.system.frame.util.LayoutUtil;


public class DrugLocateCommonDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text ypbhText;
	private Text ypmcText;
	private Text ggText;
	private Text dwText;
	private Text lsjText;
	private List<DrugsCommonBean> drugList;
	private Button okButton;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public DrugLocateCommonDialog(Shell parent, int style,List<DrugsCommonBean> drugList) {
		super(parent, style);
		setText(Messages.DrugLocateCommonDialog_0);
		this.drugList=drugList;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		Display display = getParent().getDisplay();
		LayoutUtil.centerShell(display, shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(),  SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		shell.setSize(333, 250);
		shell.setText(getText());
		{
			Composite composite = new Composite(shell, SWT.NONE);
			composite.setBackground(RcpResourceManager.getColor(
					244, 244, 247));
//			composite.setBackgroundImage(ImageUtil.toolbar);
			composite.setBackgroundMode(SWT.INHERIT_DEFAULT);
			{
				CLabel label = new CLabel(composite, SWT.NONE);
				label.setForeground(RcpResourceManager.getColor(SWT.COLOR_BLUE));
				label.setBounds(11, 14, 90, 18);
				label.setText(Messages.DrugLocateCommonDialog_1);
			}
			{
				ypbhText = new Text(composite, SWT.BORDER);
				ypbhText.addSelectionListener(new SelectionAdapter() {
					public void widgetDefaultSelected(final SelectionEvent e) {
						okButton.setFocus();
					}
				});
				ypbhText.setForeground(RcpResourceManager.getColor(SWT.COLOR_BLUE));
				ypbhText.setBounds(107, 14, 194, 18);
			}
			{
				Label label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
				label.setBounds(0, 40, 327, 2);
			}
			{
				CLabel label = new CLabel(composite, SWT.NONE);
				label.setAlignment(SWT.RIGHT);
				label.setBounds(10, 54, 90, 18);
				label.setText(Messages.DrugLocateCommonDialog_2);
			}
			{
				CLabel label = new CLabel(composite, SWT.NONE);
				label.setAlignment(SWT.RIGHT);
				label.setBounds(10, 78, 90, 18);
				label.setText(Messages.DrugLocateCommonDialog_3);
			}
			{
				CLabel label = new CLabel(composite, SWT.NONE);
				label.setAlignment(SWT.RIGHT);
				label.setBounds(10, 102, 90, 18);
				label.setText(Messages.DrugLocateCommonDialog_4);
			}
			{
				CLabel label = new CLabel(composite, SWT.NONE);
				label.setAlignment(SWT.RIGHT);
				label.setBounds(10, 126, 90, 18);
				label.setText(Messages.DrugLocateCommonDialog_5);
			}
			{
				ypmcText = new Text(composite, SWT.BORDER);
				ypmcText.setEditable(false);
				ypmcText.setBounds(106, 54, 194, 18);
			}
			{
				ggText = new Text(composite, SWT.BORDER);
				ggText.setEditable(false);
				ggText.setBounds(106, 78, 194, 18);
			}
			{
				dwText = new Text(composite, SWT.BORDER);
				dwText.setEditable(false);
				dwText.setBounds(106, 102, 194, 18);
			}
			{
				lsjText = new Text(composite, SWT.BORDER);
				lsjText.setEditable(false);
				lsjText.setBounds(106, 126, 194, 18);
			}
			{
				Label label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
				label.setBounds(0, 160, 327, 2);
			}
			{
				okButton = new Button(composite, SWT.NONE);
				okButton.setBounds(52, 180, 72, 22);
				okButton.setText(Messages.DrugLocateCommonDialog_6);
				okButton.addSelectionListener(new SelectionAdapter(){
					@Override
					public void widgetSelected(SelectionEvent e) {
						// TODO Auto-generated method stub
						super.widgetSelected(e);
						doOk();
					}
				});
			}
			{
				Button cancelButton = new Button(composite, SWT.NONE);
				cancelButton.setBounds(180, 180, 72, 22);
				cancelButton.setText(Messages.DrugLocateCommonDialog_7);
				cancelButton.addSelectionListener(new SelectionAdapter(){
					@Override
					public void widgetSelected(SelectionEvent e) {
						// TODO Auto-generated method stub
						super.widgetSelected(e);
						doCancel();
					}
				});
			}
		}
		addLinstener();
	}
	public void doOk(){
		result=ypbhText.getData();
		shell.close();
	}
	public void doCancel(){
		shell.close();
	}
	public void addLinstener(){
		CommonAssist assist = new DrugLocateCommonAssist(ypbhText, "fypbh",drugList,ypmcText,ggText,dwText,lsjText); //$NON-NLS-1$
		TextAssHelp.addContentAss(assist, ypbhText, "fypbh", false); //$NON-NLS-1$
	}
}
